Allowing Shift/Control-Clicks to Open Links in New Windows
Search Application allows users to Shift- or Control-click links to open them in a new browser window. There are a number of advantages to doing this:
- It gives the user a greater degree of control over the application and is consistent with the behavior of similar applications (such as the web browser).
- It allows the Macromedia Flash application to maintain its state. If a link is opened within the current window, the Macromedia Flash application has to unload. When users click the Back button, the browser reloads and reinitializes the application, thus losing its application state.
Adding support for Shift- and Control-clicking is actually very simple: It involves checking whether the Shift or Control keys are being pressed when a link is clicked.
Place the Code
Search Application contains multiple screens, each of which may contain other classes or components that may have links users can click. Here is where we could place the main code that handles the clicks:
- In each class that may have a clickable link
- In a global function that is accessible to all classes
- In the controller class, which listens for the events
The first option is not tenable because it would lead to extraneous and hard-to-maintain code. Creating a global function would solve the issue but it presents the problem of name space collisions (two functions having the same name) if the application is loaded into another Macromedia Flash application.
The third option is the one we use in this application. First, the application already uses an event model, with all events filtering through the main controller class. Second, because all code is maintained within the application classes, there is no chance of namespace collisions if the application is loaded within another Macromedia Flash movie.
Catching the Click
In order to use this technique, we have to know when the user has clicked a link. We could place an invisible button over a link or use HTML text and the asfunction method to call a function when a link is clicked.
Because the text and its layout are dynamic, we will use the second technique.
Let's look at a portion of HTML from the About screen class (classes/AboutScreenClass.as):
..."<a href=\"asfunction:onURLClick, http://www.google.com/apis/\">Google Web Service APIs</a>"...
This snippet of dynamic HTML creates a link to the Google web service API page. Notice that the link in the anchor () tag is not pointed at the URL directly but rather at the asfunction method.
Here is the complete anchor target:
asfunction:onURLClick, http://www.google.com/apis
The asfunction method is a built-in construct that's used like this:
asfunction:FUNCTIONNAME, FUNCTIONARGUMENTS
where FUNCTIONNAME is the name of the function that should be called and FUNCTIONARGUMENTS is the single argument passed to the function.
As you can see from the asfunction call in the code, when the user clicks the link, the onURLClickFunction is called and passed the URL to the Google website.
Here is the onURLClick function within AboutScreenClass:
AboutScreenClass.prototype.onURLClick = function(url)
{
//broadcast the click event.
this.broadcastMessage("onResultClick", url)
}
It takes one argument, a URL, and then broadcasts an onResultClick event, which indicates that a link has been clicked. It passes the URL along with the event call.
Opening the URL
What is actually listening for the event? In this case, SearchApplicationClass (classes/SearchApplicationClass.as) has registered itself as a listener for events broadcast by AboutScreenClass.
Thus when the onResultClick event is broadcast by AboutScreenClass, the onResultClick listener function in SearchApplicationClass is called:
SearchApplicationClass.prototype.onResultClick = function(url)
{
var target = "_self";
if(this.settings.openLinksInNewWindow || Key.isDown(Key.CONTROL) || Key.isDown(Key.SHIFT))
{
target = "_blank";
}
getURL(url, target);
}
This function takes the URL passed by the event and performs a getURL action with the URL.
However, it first checks to see whether the user is pressing the Control or Shift key:
Key.isDown(Key.CONTROL) || Key.isDown(Key.SHIFT)
No matter which key is pressed, it sets the target attribute for the getURL action to "_blank," which causes the URL to open in a new window. Otherwise, it uses the default value of "_self," which opens the link in the current window.
You might have also noticed this line within the IF statement:
this.settings.openLinksInNewWindow
It checks to see whether the user has specified that all links should open within a new window.
That may all seem like a lot of work just to open one link in a new window. However, it is now very easy to add Shift/Control-click support to other sections of the application. In fact, SearchScreenClass also broadcasts the onResultClick event (actually, a class within SearchScreenClass broadcasts it). This means all clicks go through this single method, which makes it very simple to change the behavior based on user input or settings.